package pms.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import pms.common.value.ProjectValue;

public class ProjectDaoImpl implements ProjectDao {

	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public List<ProjectValue> getProjectList() {
		String sql="select * from tbl_project order by project_start_dt";
		final List<ProjectValue> l_result = new ArrayList<ProjectValue>();
		jdbcTemplate.query(sql,new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				ProjectValue l_pv = new ProjectValue();
				l_pv.setProjectId(rs.getInt("PROJECT_ID"));
				l_pv.setProjectName(rs.getString("PROJECT_NAME"));
				l_pv.setProjectStartDt(rs.getDate("PROJECT_START_DT"));
				l_pv.setProjectEndDt(rs.getDate("PROJECT_END_DT"));
				l_pv.setProjectInfo(rs.getString("PROJECT_INFO"));
				l_result.add(l_pv);
			}			
		});
		return l_result;
	}


	public ProjectValue getProjectById(int id) {
		String sql="select * from tbl_project where project_id=?";
		final ProjectValue l_pv = new ProjectValue();
		jdbcTemplate.query(sql,new Object[]{new Integer(id)},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				l_pv.setProjectId(rs.getInt("PROJECT_ID"));
				l_pv.setProjectName(rs.getString("PROJECT_NAME"));
				l_pv.setProjectStartDt(rs.getDate("PROJECT_START_DT"));
				l_pv.setProjectEndDt(rs.getDate("PROJECT_END_DT"));
				l_pv.setProjectInfo(rs.getString("PROJECT_INFO"));
			}			
		});
		if(l_pv.getProjectName()==null)
			return null;
		return l_pv;
	}

}
